<?php
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');   
	class Login extends CI_Controller{
		function __construct(){
			parent::__construct();
			$this->load->library('session');
		}

		function index(){
			if(isset($this->session->userdata['manage_user'])){
				redirect('/main');exit;
			}
			if($this->input->post('username')){
				$pass = isset($_COOKIE['pass'])?$_COOKIE['pass']:"";
				if($pass){
					if($pass == $this->input->post('password')){
						$passwords = base64_decode($pass);
					}else{
					$passwords = $this->input->post('password');
					}
				}else{
					$passwords = $this->input->post('password');
				}
				$this->load->model('Logins');
				$username = $this->input->post('username');
				$check_user = $this->Logins->check_username($username);
				if(isset($check_user['id']) && $check_user['id']){
					$remember = $this->input->post('remember');
					$password = md5(md5($passwords) . $check_user['rand_str']);
					$is_disable = $this->Logins->chaeck_is_disable($check_user['id'], $password);
					if($is_disable){
						show_message('/login', "该用户已经被禁用！联系管理员！");exit;
					}
					$check_user = $this->Logins->chaeck_password($check_user['id'], $password);
					if($check_user){
						if($remember){
							setcookie('remember',$remember,time()+3600*24);
							setcookie('username',$username,time()+3600*24);
							setcookie('pass',base64_encode($passwords),time()+3600*24);
						}else{
							setcookie('remember','',time()-3600);
							setcookie('username','',time()-3600);
							setcookie('pass','',time()-3600);
						}
						$manage_user = array('uid' => $check_user['id'], 'role_id' => $check_user['role_id'], 'username' => $username);
						$this->session->set_userdata('manage_user', $manage_user);
						setcookie("uid", $check_user['id']);  
	                    setcookie("role_id",$check_user['role_id']);  
	                    setcookie("username",$username);  
						redirect('/main');
						
					}else{
						echo"<script>alert('用户密码不正确');</script>";  

					}
				}else{
					echo"<script>alert('该用户不存在');</script>";
				}
			}
			$this->smarty->assign("remember", isset($_COOKIE['remember'])?$_COOKIE['remember']:"");
			$this->smarty->assign("uname", isset($_COOKIE['username'])?$_COOKIE['username']:"");
			$this->smarty->assign("pass", isset($_COOKIE['pass'])?$_COOKIE['pass']:"");
			$this->smarty->display('login.html');
		}


		function logout(){
			$this->session->unset_userdata('manage_user');
			show_message('/login', "您已成功退出系统！");exit;
		}
	}